package com.hctforgreen.greenservice.ui.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebView;

public class ScrollWebView extends WebView {
	private OnScrollChangedCallback mOnScrollChangedCallback;
	private OnScrolledCallback mOnScrolledCallback;

	// private

	public ScrollWebView(final Context context) {
		super(context);
	}

	public ScrollWebView(final Context context, final AttributeSet attrs) {
		super(context, attrs);
	}

	public ScrollWebView(final Context context, final AttributeSet attrs,
			final int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	protected void onScrollChanged(final int l, final int t, final int oldl,
			final int oldt) {
		super.onScrollChanged(l, t, oldl, oldt);
		if (mOnScrollChangedCallback != null)
			mOnScrollChangedCallback.onScroll(l, t);
	}

	// 是否可以竖直方向滑动
	public boolean canVerti(int direction) {
		final int offset = computeVerticalScrollOffset();
		final int range = computeVerticalScrollRange()
				- computeVerticalScrollExtent();
		if (range == 0)
			return false;
		if (direction < 0) {
			return offset > 0;
		} else {
			return offset < range - 1;
		}

	}

	@SuppressLint("NewApi")
	@Override
	protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX,
			boolean clampedY) {
		// TODO Auto-generated method stub
		super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
		if (mOnScrolledCallback != null) {
			mOnScrolledCallback.onScroll(scrollX, scrollY, clampedX, clampedY);
		}
	}

	public OnScrollChangedCallback getOnScrollChangedCallback() {
		return mOnScrollChangedCallback;
	}

	public void setOnScrollChangedCallback(
			final OnScrollChangedCallback onScrollChangedCallback) {
		mOnScrollChangedCallback = onScrollChangedCallback;
	}

	/**
	 * Impliment in the activity/fragment/view that you want to listen to the
	 * webview
	 */
	public interface OnScrollChangedCallback {
		public void onScroll(int l, int t);
	}

	public void setOnScrolledCallback(
			final OnScrolledCallback onScrolledCallback) {
		mOnScrolledCallback = onScrolledCallback;
	}

	/**
	 * Impliment in the activity/fragment/view that you want to listen to the
	 * webview
	 */
	public interface OnScrolledCallback {
		public void onScroll(int scrollX, int scrollY, boolean clampedX,
				boolean clampedY);

	}

	@Override
	protected boolean canAnimate() {
		// TODO Auto-generated method stub
		return super.canAnimate();
	}

}
